package com.ruoyi.sms.util;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sms.common.SpringContextUtil;
import com.ruoyi.sms.config.SmsSendConfig;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import sun.misc.BASE64Encoder;

import java.util.*;
import java.util.stream.Stream;

/**
 * 短信签名工具类
 */
@Slf4j
public class SmsSignUtil {
    @Getter
    public enum SmsTemplateEnum {
        //cncs
        DAYS_TO_REPAYMENT("10003","100009"),
        REPAYMENT_DAY("10003","100010"),
        OVERDUE_1_DAY("10003","100011"),
        OVERDUE_3_DAY("10003","100012"),
        WITHHOLD_SUCCESS("10003","100013"),
        WITHHOLD_FAILED("10003","100014"),
        REPAYMENT_ACCOUNT("10003","100015"),
        INCREASE_CREDIT_LINE("10003","100016"),
        WITHHOLD_OUTSTANDING_SUCCESS("10003","100020"),
        WITHHOLD_OUTSTANDING_FAILED("10003","100021"),
        WITHHOLD_OVERDUE_SUCCESS("10003","100022"),
        WITHHOLD_OVERDUE_FAILED("10003","100023"),
        WITHHOLD_FINISH("10003","100024"),
        REPAYMENT_T_SUB_N("10003","200009"),
        REPAYMENT_T_SUB_1("10003","200010"),
        REPAYMENT_T_ADD_0("10003","200011"),
        REPAYMENT_T_ADD_1("10003","200012"),
        REPAYMENT_T_ADD_3("10003","200013"),
        REPAYMENT_T_ADD_5("10003","200014"),
        REPAYMENT_OUTSTANDING("10003","200015"),
        REPAYMENT_OVERDUE("10003","200016"),
        REPAYMENT_FINISH("10003","200017"),
        RAISE_LIMIT_REMIND_T_SUB_3("10003","300005"),
        RAISE_LIMIT_REMIND_T_SUB_0("10003","300006"),
        LAWERLETTERSMS("10003","220487"),
        coll_rpymt_ntce_dfr_T_ADD_1("10003","220496"),
        coll_rpymt_ntce_dfr_T_ADD_0("10003","220495"),
        coll_rpymt_ntce_dfr_T_SUB_1("10003","220494"),
        //crm
        RECALL_REGISTER_1("10002","200001"),
        RECALL_REGISTER_2("10002","200002"),
        RECALL_KYC_1("10002","200003"),
        RECALL_KYC_2("10002","200004"),
        RECALL_CREDIT_1("10002","200005"),
        RECALL_CREDIT_2("10002","200006"),
        TEMPORARY_RECALL_0("10002","200018"),
        TEMPORARY_RECALL_1("10002","200019"),
        TEMPORARY_RECALL_2("10002","200020"),
        INCREASE_DAY2("10002","200021"),
        REGISTER_RECALL("10002","200022"),
        INCREASE_DAY3("10002","200023"),
        APOLOGY_SMS("10002","200024"),
        INCREASE_DAY4("10002","200025"),
        INCREASE_DAY5("10002","200026"),
        ACTIVATION_BANDING_1("10002","200027"),
        ACTIVATION_CREDIT_1("10002","200028"),
        TEMPORARY_RECALL_CASH("10002","200029"),
        ACTIVATION_CREDIT_2("10002","200030"),
        ACTIVATION_BANDING_CREDIT_1("10002","200031"),
        ACTIVATION_BANDING_2("10002","200032"),
        ACTIVATION_CREDIT_3("10002","200033"),
        ACTIVATION_BANDING_CREDIT_2("10002","200034"),
        ACTIVATION_BANDING_3("10002","200035"),
        APP_UPGRADE("10002","200036"),
        APP_UPDATE("10002","200037"),
        RAISE_LIMIT_REMIND_NOT_LOAN("10002","300004"),
        UPGRADE_VERSION1("10002","200037"),
        UPGRADE_VERSION2("10002","200038"),
        RECALL_BINDCARD_FAIL("10002","200039"),
        INVITE_INCREASE("10002","300007"),
        UNREGISTERED_INSTALLATION("10002","300008"),
        RECALL_INSTALL_REGISTER_1("10002","300009"),
        RECALL_CARDFAILED_1("10002","300010"),
        RECALL_CARDBING_SUCCESS_1("10002","300011"),
        RECALL_INCREASE_DAY3_1("10002","300013"),
        CARDBIND_FAILED_REFUND("10002","300014"),
        RECALL_DOWNLOAD("10002","300015"),
        RECALL_CREDIT_CANCEL("10002","300018"),
        RECALL_LOAN_CANCEL("10002","300019"),
        RECALL_KYC_NOCREDIT("10002","300020"),
        RECALL_CREDIT_NOBIND("10002","300021"),
        RECALL_CARDBING_NOLOAN("10002","300022"),
        RECALL_RE_LOAN("10002","300023"),
        INVITER_REGISTER("10002","100030"),
        INVITER_CREDIT("10002","100031"),
        RECALL_WITHDRAW_CANCEL("10002","300024"),
        RECALL_INSTALL_REGISTER("10002","400001"),
        RECALL_DOB_ERROR("10002","400005"),
        RECALL_BVNERROR("10002","400006"),
        RECALL_KYCFAILED("10002","400008"),
        RECALL_CARDFAILED("10002","400010"),
        RECALL_INCREASE_DAY3("10002","400019"),
        RECALL_CARDBING_SUCCESS("10002","400009"),
        RECALL_NO_CARDBIND_WDRW("10002","300024"),
        WITHDRAW_CANCEL_RECALL("10002","100034"),
        PROMOTE_INVITATION("10002","100045"),
        RECALL_KYC_FAIL_UPDATE("10002","100047"),
        REPEAT_LENDING("10002","100048"),
        TEMP_VERIFY_FAIL_SRY("10002","100051"),
        MKT_RVW_INVT("10002","100052"),
        TEMP_LOAN_90DAYS("10002","100053"),
        LMT_INCR_WDRW_DATA("10002","100055"),
        TEMP_WDRW_CNCL_RE("10002","100040"),
        RECALL_KYC_SRY("10002","100043"),
        RECALL_INT_DECR("10002","220488"),
        TEMP_UPGRADE_ACTVLMT("10002","220490"),
        MARKET_CUST_INVIT("10002","220491"),
        ;
        private String appId;
        private String templateId;
        SmsTemplateEnum(String appId,String templateId){
            this.appId = appId;
            this.templateId = templateId;
        }
        public static SmsTemplateEnum getEnum(String templateId) {
            return Stream.of(SmsTemplateEnum.values())
                    .filter(p -> p.getTemplateId().equals(templateId))
                    .findAny()
                    .orElse(null);
        }
    }

    /** 主业务系统以外的系统标识跟验签配置 **/
    public static final Map<String,SmsSendConfig> appIdMap = new HashMap<String,SmsSendConfig>(){{
        SmsSendConfig crmConfig = new SmsSendConfig();
        crmConfig.setAppId("10002");
        crmConfig.setSign("lcredit_market");
        SmsSendConfig cncsConfig = new SmsSendConfig();
        cncsConfig.setAppId("10003");
        cncsConfig.setSign("cncs");
        put("10002",crmConfig);
        put("10003",cncsConfig);
    }};

    /**
     * BASE64加密
     */
    public JSONObject encryptBASE64(String templateId){
        //默认的
        SmsSendConfig smsSendConfig = SpringContextUtil.getBean(SmsSendConfig.class);
        String appId = smsSendConfig.getAppId();
        String apiKey = smsSendConfig.getSign();
        if(!StringUtils.isEmpty(templateId)){
            SmsTemplateEnum smsTemplateEnum = SmsTemplateEnum.getEnum(templateId);
            if(smsTemplateEnum!=null){
                SmsSendConfig smsSendConfigNew = appIdMap.get(smsTemplateEnum.getAppId());
                appId = smsSendConfigNew.getAppId();
                apiKey = smsSendConfigNew.getSign();
            }
        }
        JSONObject jsonObject = new JSONObject();
        long timestamp = getSecondTimestampTwo(new Date());
        String param = "appId="+ appId +
                "&apiKey="+ apiKey +"&timestamp="+timestamp;
        log.info("-----param :" + param);
        String sign = (new BASE64Encoder()).encodeBuffer(param.getBytes());
        jsonObject.put("appId",appId);
        jsonObject.put("timestamp",timestamp);
        jsonObject.put("sign",sign);
        return jsonObject;
    }

    /**
     * BASE64加密
     */
    public JSONObject encryptBASE64(SmsSendConfig paramConfig){
        //默认的
        SmsSendConfig smsSendConfig = SpringContextUtil.getBean(SmsSendConfig.class);
        if(paramConfig!=null){
            smsSendConfig = paramConfig;
        }
        String appId = smsSendConfig.getAppId();
        String apiKey = smsSendConfig.getSign();
        JSONObject jsonObject = new JSONObject();
        long timestamp = getSecondTimestampTwo(new Date());
        String param = "appId="+ appId +
                "&apiKey="+ apiKey +"&timestamp="+timestamp;
        log.info("-----param :" + param);
        String sign = (new BASE64Encoder()).encodeBuffer(param.getBytes());
        jsonObject.put("appId",appId);
        jsonObject.put("timestamp",timestamp);
        jsonObject.put("sign",sign);
        return jsonObject;
    }

    /**
     * BASE64加密
     */
    public JSONObject encryptBASE64(){
        SmsSendConfig smsSendConfig = SpringContextUtil.getBean(SmsSendConfig.class);
        JSONObject jsonObject = new JSONObject();
        long timestamp = getSecondTimestampTwo(new Date());
        String param = "appId="+smsSendConfig.getAppId() +
                "&apiKey="+smsSendConfig.getSign()+"&timestamp="+timestamp;
        log.info("-----param :" + param);
        String sign = (new BASE64Encoder()).encodeBuffer(param.getBytes());
        jsonObject.put("appId",smsSendConfig.getAppId());
        jsonObject.put("timestamp",timestamp);
        jsonObject.put("sign",sign);
        return jsonObject;
    }

    /**
     * 获取精确到秒的时间戳
     * @param date
     * @return
     */
    public static long getSecondTimestampTwo(Date date){
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime());
        return Long.valueOf(timestamp);
    }
}
